import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@WebServlet("/LoginCheckServlet")
public class LoginCheckServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收账号，密码，验证码
        String username=req.getParameter("username");
        String password=req.getParameter("password");
        String userCode=req.getParameter("userCode");
        //从session中取出正确验证码
        HttpSession session=req.getSession();
        String vCode= (String) session.getAttribute("vCode");
        //将用户输入的验证码和从session中的正确验证码进行对比
        resp.setContentType("text/html;charset=UTF-8");
        ServletOutputStream outputStream=resp.getOutputStream();
        if (userCode.equals(vCode)) {
            if ("admin".equals(username)&&"admin".equals(password)){

                outputStream.write("登陆成功".getBytes("UTF-8"));
            }else {

                outputStream.write("登陆失败,密码错误".getBytes("UTF-8"));
            }
        }else {

            outputStream.write("登陆失败，验证码错误".getBytes("UTF-8"));
        }

    }
}